package cn.iocoder.yudao.module.shop.service.priceitem;

import cn.iocoder.yudao.module.shop.dal.dataobject.price.ShopPriceDO;
import cn.iocoder.yudao.module.shop.dal.mysql.price.ShopPriceMapper;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.priceitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.priceitem.ShopPriceItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.priceitem.ShopPriceItemConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.priceitem.ShopPriceItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 价格单项 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopPriceItemServiceImpl implements ShopPriceItemService {

    @Resource
    private ShopPriceItemMapper priceItemMapper;


    @Resource
    private ShopPriceMapper shopPriceMapper;

    @Autowired
    private ShopItemService shopItemService;

    @Override
    public Long createPriceItem(ShopPriceItemCreateReqVO createReqVO) {

        if(shopPriceMapper.selectOne(ShopPriceDO::getId, createReqVO.getPriceId()) == null){
            throw exception(PRICE_NOT_EXISTS);
        }

        if(shopItemService.getItem(createReqVO.getItemId()) == null){
            throw exception(ITEM_NOT_EXISTS);
        }

        if(createReqVO.getPrice() < 0){
            throw exception(PRICE_INVALID);
        }

        // 插入
        ShopPriceItemDO priceItem = ShopPriceItemConvert.INSTANCE.convert(createReqVO);
        priceItemMapper.insert(priceItem);
        // 返回
        return priceItem.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createPriceItemBatch(ShopPriceItemBatchCreateReqVO createReqVO){
        for(ShopPriceItemCreateReqVO createItem : createReqVO.getItems()){
            createPriceItem(createItem);
        }

        return true;
    }

    @Override
    public void updatePriceItem(ShopPriceItemUpdateReqVO updateReqVO) {
        // 校验存在
        validatePriceItemExists(updateReqVO.getId());
        // 更新
        ShopPriceItemDO updateObj = ShopPriceItemConvert.INSTANCE.convert(updateReqVO);
        priceItemMapper.updateById(updateObj);
    }

    @Override
    public void deletePriceItem(Long id) {
        // 校验存在
        validatePriceItemExists(id);
        // 删除
        priceItemMapper.deleteById(id);
    }

    private void validatePriceItemExists(Long id) {
        if (priceItemMapper.selectById(id) == null) {
            throw exception(PRICE_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public ShopPriceItemDO getPriceItem(Long id) {
        return priceItemMapper.selectById(id);
    }

    @Override
    public ShopPriceItemDO getPriceItem(Long priceId, Long itemId) {
        return priceItemMapper.selectOne(ShopPriceItemDO::getPriceId, priceId, ShopPriceItemDO::getItemId, itemId);
    }

    @Override
    public List<ShopPriceItemDO> getPriceItemList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return priceItemMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopPriceItemDO> getPriceItemListByPriceId(Collection<Long> priceIdList){
        return priceItemMapper.selectList(ShopPriceItemDO::getPriceId, priceIdList);
    }

    @Override
    public List<ShopPriceItemDO> getPriceItemListByPriceIdAndItemId(Collection<Long> priceIdList, Collection<Long> itemIdList){
        if(priceIdList.size() == 0 || itemIdList.size() == 0){
            return new ArrayList<>();
        }

        return priceItemMapper.selectList(new LambdaQueryWrapper<ShopPriceItemDO>()
                .in(ShopPriceItemDO::getPriceId, priceIdList)
                .in(ShopPriceItemDO::getItemId, itemIdList));
    }

    @Override
    public PageResult<ShopPriceItemDO> getPriceItemPage(ShopPriceItemPageReqVO pageReqVO) {
        return priceItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopPriceItemDO> getPriceItemList(ShopPriceItemExportReqVO exportReqVO) {
        return priceItemMapper.selectList(exportReqVO);
    }

}
